﻿using System;
using System.Collections;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Xml;

namespace TextEditor
{


    public class ZYAttributeCollection : CollectionBase
    {
        private bool bolModified = false;
        private ArrayList myList = new ArrayList();
        public ZYTextElement OwnerElement;

        public new void Clear()
        {
            this.myList.Clear();
            this.bolModified = true;
        }

        public bool Contains(string strName)
        {
            foreach (ZYAttribute attribute in this.myList)
            {
                if (attribute.Name == strName)
                {
                    return true;
                }
            }
            return false;
        }

        public void CopyTo(ZYAttributeCollection descSet)
        {
            ArrayList list = descSet.getInnerList();
            list.Clear();
            foreach (ZYAttribute attribute in this.myList)
            {
                list.Add(attribute.Clone());
            }
        }

        public bool EqualsValue(ZYAttributeCollection a)
        {
            if (a == null)
            {
                return false;
            }
            return ((a == this) || (this.ToListString() == a.ToListString()));
        }

        public bool FromXML(XmlElement myElement)
        {
            this.myList.Clear();
            if (myElement == null)
            {
                return false;
            }
            foreach (XmlAttribute attribute in myElement.Attributes)
            {
                ZYAttribute attribute2 = new ZYAttribute();
                attribute2.Name = attribute.Name;
                attribute2.ValueString = attribute.Value;
                if (attribute2.Value != attribute2.DefaultValue)
                {
                    this.myList.Add(attribute2);
                }
            }
            this.bolModified = false;
            return true;
        }

        public bool GetBool(string strName)
        {
            foreach (ZYAttribute attribute in this.myList)
            {
                if (attribute.Name == strName)
                {
                    if (attribute.Value.ToString() == "True")
                        return true;
                    else if (attribute.Value.ToString() == "False")
                        return false;
                    else
                        return (bool)attribute.Value;
                }
            }
            return (bool)ZYAttribute.GetDefaultValue(strName);
        }

        public Color GetColor(string strName)
        {
            foreach (ZYAttribute attribute in this.myList)
            {
                if (attribute.Name == strName)
                {
                    return (Color)attribute.Value;
                }
            }
            return SystemColors.WindowText;
        }

        public float GetFloat(string strName)
        {
            try
            {
                foreach (ZYAttribute attribute in this.myList)
                {
                    if (attribute.Name == strName)
                    {
                        return Convert.ToSingle(attribute.Value);
                    }
                }
                return (float)ZYAttribute.GetDefaultValue(strName);
            }
            catch
            {
                return 0f;
            }
        }

        internal ArrayList getInnerList()
        {
            return this.myList;
        }

        public int GetInt32(string strName)
        {
            foreach (ZYAttribute attribute in this.myList)
            {
                if (attribute.Name == strName)
                {
                    return Convert.ToInt32(attribute.Value);
                }
            }
            return 0;
        }

        public string GetString(string strName)
        {
            foreach (ZYAttribute attribute in this.myList)
            {
                if (attribute.Name == strName)
                {
                    return Convert.ToString(attribute.Value);
                }
            }
            return Convert.ToString(ZYAttribute.GetDefaultValue(strName));
        }

        public void RemoveAttribute(string strName)
        {
            ZYAttribute attribute = this[strName];
            if (attribute != null)
            {
                this.myList.Remove(attribute);
            }
        }

        public bool SetDefaultValue()
        {
            bool flag = false;
            foreach (ZYAttribute attribute in this.myList)
            {
                object defaultValue = ZYAttribute.GetDefaultValue(attribute.Name);
                if ((defaultValue != null) && defaultValue.Equals(attribute.Value))
                {
                    if (((this.OwnerElement != null) && (this.OwnerElement.OwnerDocument != null)) && (this.OwnerElement.OwnerDocument.ContentChangeLog != null))
                    {
                        this.OwnerElement.OwnerDocument.ContentChangeLog.LogAttribute(this.OwnerElement, attribute, defaultValue);
                    }
                    attribute.Value = defaultValue;
                    this.bolModified = true;
                    flag = true;
                }
            }
            return flag;
        }

        public void SetValue(string strName, object vValue)
        {
            ZYAttribute attribute = null;
            foreach (ZYAttribute attribute2 in this.myList)
            {
                if (attribute2.Name == strName)
                {
                    attribute = attribute2;
                    break;
                }
            }
            if (attribute == null)
            {
                attribute = new ZYAttribute();
                attribute.Name = strName;
                this.myList.Add(attribute);
            }
            if (((attribute.Value == null) || !attribute.Value.Equals(vValue)) && ((attribute.Value != null) || (vValue != null)))
            {
                if (((this.OwnerElement != null) && (this.OwnerElement.OwnerDocument != null)) && (this.OwnerElement.OwnerDocument.ContentChangeLog != null))
                {
                    this.OwnerElement.OwnerDocument.ContentChangeLog.LogAttribute(this.OwnerElement, attribute, vValue);
                    this.OwnerElement.OwnerDocument.Modified = true;
                }
                attribute.Value = vValue;
                this.bolModified = true;
            }
        }

        public string ToListString()
        {
            StringBuilder builder = new StringBuilder();
            foreach (ZYAttribute attribute in this.myList)
            {
                if (attribute.isNeedSave())
                {
                    string valueString = attribute.ValueString;
                    if (valueString != null)
                    {
                        builder.Append(attribute.Name + "=" + valueString);
                    }
                }
            }
            return builder.ToString();
        }

        public bool ToXML(XmlElement myElement)
        {
            if (myElement == null)
            {
                return false;
            }
            foreach (ZYAttribute attribute in this.myList)
            {
                if (attribute.isNeedSave() && (attribute.ValueString != null))
                {
                    myElement.SetAttribute(attribute.Name, attribute.ValueString);
                }
            }
            if (this.Contains("xmlfontname"))
            {
                myElement.SetAttribute("fontname", this.GetString("xmlfontname"));
            }
            return true;
        }

        public new int Count
        {
            get
            {
                return this.myList.Count;
            }
        }

        public ZYAttribute this[string strName]
        {
            get
            {
                foreach (ZYAttribute attribute in this.myList)
                {
                    if (attribute.Name == strName)
                    {
                        return attribute;
                    }
                }
                return null;
            }
        }

        public bool Modified
        {
            get
            {
                return this.bolModified;
            }
            set
            {
                this.bolModified = value;
            }
        }
    }
}

